package com.example.wifi4client;

import android.os.Handler;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ConnectTask implements Runnable {
    public Socket mSocket;
    private Handler mHandler;

    public ReadTask mReadTask;



    public String mHost;
    public int mPort;

    public ConnectTask(String host, int port,Handler handler){
        mSocket = null;
        mHost = host;
        mPort = port;

        mHandler = handler;
        mReadTask = null;
    }

    @Override
    public void run() {
        try {
            mSocket = new Socket(mHost,mPort);

            if (mSocket == null ||
                mSocket.isConnected() == false ||
                mSocket.isClosed() == true){
                mHandler.sendEmptyMessage(MainActivity.DEVICE_CONNECTED);
                return;
            }

            InputStream inputStream = mSocket.getInputStream();
            mReadTask = new ReadTask(mSocket,mHandler);
            ThreadPoolManager.THREAD_POOL_EXECUTOR.execute(mReadTask);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
